package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.AmenityLogic;

import data.Amenity;
import data.passingData.ResultData;

@SuppressWarnings("serial")

public class AmenityService extends HttpServlet {
	
	//http://localhost:8080/amenityService?func=addAmenity&description=test&pic=urlpic&amenities=book
	//http://localhost:8080/amenityService?func=editAmenity&amenId=826&description=test1&pic=urlpic1&amenities=book1
	//http://localhost:8080/amenityService?func=delAmenity&amenId=826
	//http://localhost:8080/amenityService?func=changeAmenity&amenId=826
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addAmenity")){
				AddAmenity(req,resp);
			}else if(doClass.equals("editAmenity")){
				EditAmenity(req,resp);
			}else if(doClass.equals("delAmenity")){
				DeleteAmenity(req,resp);
			}else if(doClass.equals("changeAmenity")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getAllAmenities")){
				GetAllAmenities(req,resp);
			}else if(doClass.equals("getAmenityById")){
				GetAmenityById(req,resp);
			}else if(doClass.equals("getShowAmenities")){
				GetShowAmenity(req,resp);
			}else if(doClass.equals("editSortOrder")){
				EditSortOrder(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private void AddAmenity(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String description = req.getParameter("description");
		String pic = req.getParameter("pic");
		String amenities = req.getParameter("title");
		
		new AmenityLogic().AddAmenity(hotelId,description,pic,amenities);
		
		GetAllAmenities(req, resp);	
	}

	private void EditAmenity(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long amenId = Long.parseLong(req.getParameter("id"));
		String description = req.getParameter("description");
		String pic = req.getParameter("pic");
		String amenities = req.getParameter("title");

		new AmenityLogic().EditAmenity(amenId,description,pic,amenities);
		
		GetAllAmenities(req, resp);	
	}

	private void EditSortOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long amenId = Long.parseLong(req.getParameter("id"));
		long sortOrder = Long.parseLong(req.getParameter("sortOrder"));

		Amenity a = new AmenityLogic().EditSortOrder(amenId, sortOrder);
		String x = "T";
		if(a == null){
			x = "F";
		}

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(x));
	}
	
	private void DeleteAmenity(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String amenIdList = req.getParameter("amenId");
		for(int i = 0; i < amenIdList.split(",").length ; i++){
			new AmenityLogic().DeleteAmenity(Long.parseLong(amenIdList.split(",")[i]));
		}

		GetAllAmenities(req, resp);	
	}

	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long amenId = Long.parseLong(req.getParameter("amenId"));

		Object obj = new AmenityLogic().ChangeStatus(amenId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllAmenities(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<Amenity> obj = new AmenityLogic().GetAllAmenity4AdminLimit(hotelId,offset,line);
		double dataSize = new AmenityLogic().GetAllAmenity4Admin(hotelId).size();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetAmenityById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		Amenity obj = new AmenityLogic().GetAmenity(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetShowAmenity(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<Amenity> obj = new AmenityLogic().GetAllAmenity(hotelId);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
}

